/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package miniprojet.Models;

/**
 * Describe class Player here.
 *
 *
 * Created: Tue Mar 23 10:44:18 2010
 *
 * @author <a href="mailto:stg1@pc111-1.formation-continue.grenoble-inp.fr">stagiaire</a>
 * @version 1.0
 */

import java.io.*;

public final class Player {

    /**
     * Creates a new <code>Player</code> instance.
     *
     */
    InputStream fis;
    BufferedOutputStream aos;

    public Player(int freq, int nbcanaux, boolean signe, int nboctet, InputStream is) {
	try{
	    String cmd = String.format("play -r %d -c %1d %s -%1d -t raw -",freq, nbcanaux, signe ? "-s" : "", nboctet);
	    System.out.println(cmd);
	    Process play = Runtime.getRuntime().exec(cmd);
	    aos = new BufferedOutputStream(play.getOutputStream());
	    fis = is;
	}catch(Exception e){
	    System.out.println(e);
	}
    }

    public void play(){
	byte [] buf = new byte[640];
	int n;
	try{
	    n = fis.read(buf);
	    while (n>=0){
		aos.write(buf,0,n);
		n = fis.read(buf);
	    }
	}catch(Exception e){
	    System.out.println(e);
	}
    }
    

}
